<?php
namespace app\app\controller\v1;
use think\Db;
use think\Request;
use app\app\util\OnlineUtils;
use app\app\util\IntUtils;

class UserController extends BaseController{

    /**
     * @return \think\Response
     * 用户信息
     */
    public function UserInfo()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $data = Db::name('user')->find($user_id);
        return json_code('200','success',$data);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     * 
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
    	
    }

    /**
     * 显示指定的资源
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id)
    {

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {

    }

    /**
     * [getUserInfo 获取个人信息]
     * @return [json] [description]
     */
    public function getUserInfo()
    {
        $user_id = OnlineUtils::getUserIdBySid();
       
        $user    = Db::name('user')->field('id,nickname,avatar_url,integral,times,victory_times')->where('id','eq',$user_id)->where('status','eq',1)->find();
        $card    = Db::name('my_card')->field('sum(num) num')->where('user_id','eq',$user_id)->find();
        $user['card_num'] = intval($card['num']);
        //胜率
        if($user['times'] == 0){
            $user['rate'] = 0;
        }else{
            $user['rate'] = (ceil(($user['victory_times']/$user['times'])*100))."%";
        }
        return ft(200,'success',['data'=>$user]);
    }

    //排行榜
    public function RankList()
    {
        //分页数据
        if(input('page') && input('page')>0){
            $page = intval(input('page'));
            $start= $page*config('PAGE_NUM') + 1;
        }else{
            $page = 1;
            $start= 0;
        }
        $end   =$start + intval(config('PAGE_NUM'))-1;

        //取出排行榜
        $int  = IntUtils::getIntRank($start,$end);
        if( empty($int)){
            $data = [];
        }
        //处理数据
        foreach($int as $k => $v){
            $data[$k] = Db::name('user')->field('id,nickname,avatar_url,integral')->where('id','eq',$v)->find();
        }
        return ft(200,'success',['start'=>$start,'end'=>$end,'data'=>$data]);
    }




    /**
     * [message 获取access_token]
     * @return [type] [description]
     */
    private function getAccessToken()
    {
        //请求地址
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".config('APPID')."&secret=".config('SECRET');
        //数据
        $result = makeRequest($url,array());
        $result = json_decode($result);
        $access_token = $result->access_token;
        return $access_token;
    }




    public function getwxaqrcode()
    {
        $access_token = $this->AccessToken();
        $url   = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
        $path  = "pages/fans/fans";
        $width = 430;
        $data  = '{"path":"'.$path.'","width":'.$width.'}';
        $return= $this->request_post($url,$data);
        //将生成的小程序码存入相应文件夹下
        $name = microtime();
        file_put_contents(ROOT_PATH.'public'.DS.'/static/code/'.$name.'.png',$return);
        return ft(200,'success',['data'=>config('CODE').$name.'.png']);
    }

    public function AccessToken()
    {
       $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.config('APPID').'&secret='.config('SECRET');
        $AccessToken = $this->request_post($url);
        $AccessToken = json_decode($AccessToken , true);
        $AccessToken = $AccessToken['access_token'];
        return $AccessToken;
    }

    public function request_post($url, $data=array()){
            $ch = curl_init();
            $header = array("Accept-Charset"=>"utf-8");
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $tmpInfo = curl_exec($ch);
            if (curl_errno($ch)) {
                return false;
            }else{
                return $tmpInfo;
            }
    }


}